/********************************************************************************************************
 * PROGRAM      : TM
 * DATE - TIME  : Friday 04 August 2006 - 19h18
 * AUTHOR       :  (  )
 * FILENAME     : main.cpp
 * LICENSE      : 
 * COMMENTARY   : 
 ********************************************************************************************************/
#include <QCoreApplication>
#include <QtDebug>
#include "qextserialport.h"
#include <QDataStream>
#include <QSettings>
#include <time.h>
#include "globals.h"
#include "mcamconblock.h"
using namespace TM;
int main( int argc, char ** argv )
{
        unsigned int cnt=0;
       
        QString dev;
        QCoreApplication app( argc, argv );
        if(argc)
         dev = argv[1];
        if(dev.isEmpty())
         dev = "/dev/ttyS0";
        qWarning()<<"opening "<<dev;
        QextSerialPort serial(dev);
        serial.setBaudRate(BAUD57600);
        serial.setDataBits(DATA_8);
        serial.setParity(PAR_NONE);
        serial.setStopBits(STOP_1);
        serial.setFlowControl(FLOW_OFF);
        if(!serial.open(QIODevice::WriteOnly | QIODevice::Unbuffered))
        {
          qWarning()<<"Telemetry receiver coulnd not open serial port "<<serial.portName();
          exit(0);
        }  
      
        QDataStream s(&serial);
        MDataBlock block(MDataBlock::CAMCON);
        quint32 sync = 0xa5a5a5a5;
        while(1)
        {
         //char A = 'A';
         //s<<cnt++;
         MDataBlock::TYPE type = block.type();
         block.setBlockNum(cnt++);
         serial.writeData((char*)&sync,sizeof(sync));
         serial.writeData((char *)&block,sizeof(block));
         //serial.writeData(&A,sizeof(block));
         ///s<<&block;
         //s<<'A';
         serial.flush();
//         s.device()->flush();
         serial.waitForBytesWritten(-1);
         qDebug()<<QString::number(cnt);
         if(cnt==11)cnt=0;
         usleep(100000); //wait 100 millisec
        }
        
	return app.exec();
}
